package com.qf.fmw.dao.impl;

import com.qf.fmw.dao.GoodsTypeDao;
import com.qf.fmw.entity.bo.GoodsType;
import com.qf.fmw.entity.bo.ProductBo;
import com.qf.fmw.utils.DBManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {

    // 查询所有商品类型
    @Override
    public List<GoodsType> selectAllGoodsTypes() {
        return DBManager.commonQuery("select t_id,t_name,t_level,t_belong_type from type where  type.is_delete = 1", GoodsType.class);
    }

    //根据级别和名称查询商品类型
    @Override
    public List<GoodsType> searchGoodsTypes(String level, String name) {
        return DBManager.commonQuery("select t_id,t_name,t_level,t_belong_type from type where t_level = ? and is_delete = 1 and t_name like ?", GoodsType.class, level, "%" + name + "%");
    }

    //根据级别查询商品类型
    @Override
    public List<GoodsType> searchGoodsTypesByLevel(String level) {
        return DBManager.commonQuery("select t_id,t_name,t_level,t_belong_type from type where is_delete = 1 and t_level = ?", GoodsType.class, level);
    }

    //根据名称查询商品类型
    @Override
    public List<GoodsType> searchGoodsTypesByName(String name) {
        return DBManager.commonQuery("select t_id,t_name,t_level,t_belong_type from type where is_delete = 1 and t_name like ?", GoodsType.class, "%" + name + "%");
    }
    @Override
    public int insertGoods(String name,Date pubdate, String price, String star, String intro, String picture) {
        return DBManager.commonUpdate("INSERT INTO product(p_name,p_time,p_image,p_price,p_state,p_info) VALUES (?,?,?,?,?,?)", name, pubdate, picture, price, star, intro);
    }

    //修改商品类型
    @Override
    public int updateGoodsType(int typeId, String name, String level, String belongType) {
        return DBManager.commonUpdate("UPDATE type SET t_name = ?, t_level = ?, t_belong_type = ? WHERE t_id = ?", name, level, belongType, typeId);
    }


//删除商品
    @Override
    public int deleteGoods(int pId) {
        return DBManager.commonUpdate("UPDATE product SET is_delete = 0 WHERE p_id = ?", pId);
    }

    // 修改商品
    @Override
    public int updateGoods(int pId, String pName, String pPrice, String pTime, String pInfo) {
        return DBManager.commonUpdate("UPDATE product SET p_name = ?, p_price = ?, p_time = ?, p_info = ? WHERE p_id = ?", pName, pPrice, pTime, pInfo, pId);
    }



    // 删除商品类型
    @Override
    public int deleteGoodsType(int typeId) {
        return DBManager.commonUpdate("UPDATE type SET is_delete = 0 WHERE t_id = ?", typeId);
    }

    //展示商品列表
    @Override
    public List<ProductBo> showGoodsTypes() {
        return DBManager.commonQuery("SELECT p_id,p_name,p_price,p_time,p_info,t_name FROM type,product WHERE type.t_id=product.t_id AND product.is_delete = 1;", ProductBo.class);
    }
//     搜索商品
    @Override
    public List<ProductBo> searchGoods(String name, String pubdate) {
        String sql = "SELECT p_id, p_name, p_price, p_time, p_info, t_name FROM product, type WHERE product.t_id = type.t_id AND product.is_delete = 1";
        List<Object> params = new ArrayList<>();

        if (name != null && !name.isEmpty()) {
            sql += " AND p_name LIKE ?";
            params.add("%" + name + "%");
        }
        if (pubdate != null && !pubdate.isEmpty()) {
            sql += " AND p_time = ?";
            params.add(pubdate);
        }

        return DBManager.commonQuery(sql, ProductBo.class, params.toArray());
    }

    //展示描述内容
    @Override
    public String getGoodsInfo(int id) {
        return (String) DBManager.commonQuery("SELECT p_info FROM product WHERE p_id = ?", id);
    }


}



